from django.contrib import admin
from readers.models import *
# from readers.utils import  QuerySetSequence

class ReservingInline(admin.StackedInline):
	model = Reserving
	extra = 0

	def queryset(self, request):
		qs = Reserving.objects.filter(status='C')
		return qs

class ReadingInline(admin.StackedInline):
	model = Reading
	extra = 0

	def queryset(self, request):
		qs = Reading.objects.filter(status='C')
		return qs

class ReadingHistoryInline(admin.TabularInline):
	model = Reading
	extra = 0

	def queryset(self, request):
		qs = Reading.objects.filter(status='A' or 'B')
		return qs

class ReservingHistoryInline(admin.TabularInline):
	model = Reserving
	extra = 0

	def queryset(self, request):
		qs = Reserving.objects.filter(status='A' or 'B')
		return qs

class UserProfileAdmin(admin.ModelAdmin):
	inlines = [ReadingInline, ReservingInline, ReadingHistoryInline, ReservingHistoryInline]
	list_display = ['user', 'department']

class BookProfileAdmin(admin.ModelAdmin):
	inlines = [ReadingInline, ReservingInline]
	list_display = ['book', 'num_available', 'num_total', 'num_reserving']

	def save_model(self, request, obj, form, change):
		# initial
		if obj.num_total == -1:
			obj.num_total = obj.book.num_copy
			obj.num_available = obj.num_total
			obj.num_reserving = 0

		obj.save()

admin.site.register(UserProfile, UserProfileAdmin)
admin.site.register(BookProfile, BookProfileAdmin)
